package com.stylefeng.guns.modular.system.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.SqlRunner;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.annotion.Permission;
import com.stylefeng.guns.common.annotion.log.BussinessLog;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.persistence.model.CpciaNews;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.dao.LogDao;
import com.stylefeng.guns.modular.system.service.ICpciaNewsService;
import com.stylefeng.guns.modular.system.service.ICpciaNewsTaskService;
import com.stylefeng.guns.modular.system.service.impl.CpciaNewsTaskImpl;
import com.stylefeng.guns.modular.system.warpper.LogWarpper;

/**
 * 日志管理的控制器
 *
 * @author fengshuonan
 * @Date 2017年4月5日 19:45:36
 */
@Controller
@RequestMapping("/cpciaNews")
public class CpciaNewsController extends BaseController {

    private static String PREFIX = "/system/cpcia/";

    @Resource
    private LogDao logDao;
    
    @Resource
    private ICpciaNewsService cpciaNewsService;
    
    @Resource
    private ICpciaNewsTaskService cpciaNewsTaskService;

    /**
     * 跳转到日志管理的首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "cpcia_news.html";
    }

    /**
     * 查询登录日志列表
     */
    @RequestMapping("/list")
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object list() {
        Page<CpciaNews> page = new PageFactory<CpciaNews>().defaultPage();
        List<Map<String, Object>> result = cpciaNewsService.getCpciaNews(page);
        page.setRecords((List<CpciaNews>) new LogWarpper(result).warp());
        return super.packForBT(page);
    }
    
    /**
     * 查询登录日志列表
     */
    @RequestMapping("/updateSpiderResults")
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public String updateSpiderResults() {
    	cpciaNewsTaskService.cpciaNewsTask();
    	return PREFIX + "cpcia_news.html";
    }


    /**
     * 跳转到修改通知
     */
    @RequestMapping("/cpciaNews_detail/{newsID}")
    public String cpciaNewsDetail(@PathVariable String newsID, Model model) {
        CpciaNews cpciaNews = new CpciaNews();
        cpciaNews = cpciaNewsService.selectById(newsID);
        model.addAttribute("cpciaNews",cpciaNews);
        LogObjectHolder.me().set(cpciaNews);
        return PREFIX + "cpciaNews_detail.html";
    }
    
    /**
     * 清空日志
     */
    @BussinessLog("清空登录日志")
    @RequestMapping("/delLoginLog")
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object delLog() {
        SqlRunner.db().delete("delete from login_log");
        return super.SUCCESS_TIP;
    }
}
